package wxapp

import (
    "fmt"
	"errors"
	"wxsdk/miniapp/core"
	"encoding/json"
	"github.com/astaxie/beego"
)

// 修改小程序线上代码的可见状态（仅供第三方代小程序调用）
type ChangeVisitStatusRequest struct {
	Action    string `json:"action"`
}

func (r *ChangeVisitStatusRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/change_visitstatus?access_token=%s",token)
}


type ChangeVisitStatusResponseResult struct {
	*core.ErrorResponse
}

// 为授权的小程序帐号上传小程序代码
type CommitRequest struct {
	TemplateId  int    `json:"template_id"`
	ExtJson     string `json:"ext_json"`
    UserVersion string `json:"user_version"`
	UserDesc    string `json:"user_desc"`
}

func (r *CommitRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/commit?access_token=%s",token)
}

type CommitResponseResult struct {
	*core.ErrorResponse
}

// 获取第三方提交的审核版本的审核状态（仅供第三方代小程序调用）
type GetAuditStatusRequest struct {
	AuditId  int    `json:"auditid"`
}

func (r *GetAuditStatusRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/get_auditstatus?access_token=%s",token)
}

type GetAuditStatusResponseResult struct {
	*core.ErrorResponse
	*GetAuditStatusResponse
}

type GetAuditStatusResponse struct {
	Status   int    `json:"status"`
	Reason   string `json:"reason,omitempty"`
	Auditid  int64  `json:"auditid,omitempty"`
}

// 获取授权小程序帐号的可选类目
type GetCategoryRequest struct {
	
}

func (r *GetCategoryRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/get_category?access_token=%s",token)
}

type GetCategoryResponseResult struct {
	*core.ErrorResponse
	*GetCategoryResponse
}

type GetCategoryResponse struct {
	CategoryList    []CategoryList `json:"category_list"`
}

type CategoryList struct {
	FirstClass   string   `json:"first_class"`
	SecondClass  string   `json:"second_class"`
	ThirdClass   string   `json:"third_class"`
}

// 获取授权小程序帐号的可选类目
type GetPageRequest struct {
	
}

func (r *GetPageRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/get_page?access_token=%s",token)
}

type GetPageResponseResult struct {
	*core.ErrorResponse
	*GetPageResponse
}

type GetPageResponse struct {
	PageList   []string `json:"page_list"`
}

// 获取体验小程序的体验二维码
type GetQrcodeRequest struct {
	
}

func (r *GetQrcodeRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/get_qrcode?access_token=%s",token)
}


// 发布已通过审核的小程序（仅供第三方代小程序调用）
type ReleaseRequest struct {
	
}

func (r *ReleaseRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/release?access_token=%s",token)
}

type ReleaseResponseResult struct {
	*core.ErrorResponse
}

// 将第三方提交的代码包提交审核（仅供第三方开发者代小程序调用）
type SubmitAuditRequest struct {
	ItemLists []*ItemList `json:"item_list"`
	//Data         string   `json:"-"`
}

type ItemList struct {
	Address      string    `json:"address"`
	Tag          string    `json:"tag"`
	FirstClass   string    `json:"first_class"`
	SecondClass  string    `json:"second_class"`
	ThirdClass   string    `json:"third_class"`
	Title        string    `json:"title"`
}

func (r *SubmitAuditRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/submit_audit?access_token=%s",token)
}
type SubmitAuditResponseResult struct {
	*core.ErrorResponse
	*SubmitAuditResponse
}

type SubmitAuditResponse struct {
	AuditId  int `json:"auditid"`
}

// 为授权的小程序帐号上传小程序代码
func  CommitCode(authorizerAccessToken string,templateId int,extJson,userVersion,userDesc string) (int, error){
	client := core.GetPostJSONClient()
	var rsp CommitResponseResult
	var req = new(CommitRequest)
	req.TemplateId = templateId
	req.ExtJson = extJson
	req.UserVersion = userVersion
	req.UserDesc = userDesc
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("POST", serverUrl, req, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return -5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return 0, nil
}

// 获取体验小程序的体验二维码

func  GetQrcode(authorizerAccessToken string) ([]byte, int, error){
	client := core.GetPostJSONClient()
	var req = new(GetQrcodeRequest)
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("GET", serverUrl, nil, nil)
	if err != nil {
		beego.Critical(err.Error())
		return []byte{}, -5, err
	}
	return tbdata, 0, nil
}

// 获取授权小程序帐号的可选类目
func  GetCategory(authorizerAccessToken string) (string, int, error){
	client := core.GetPostJSONClient()
	var rsp GetCategoryResponseResult
	var req = new(GetCategoryRequest)
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("GET", serverUrl, nil, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "",-5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return string(tbdata),0, nil
}

// 将第三方提交的代码包提交审核（仅供第三方开发者代小程序调用）
func  SubmitAudit(authorizerAccessToken, data string) (int, int, error){
	client := core.GetPostJSONClient()
	var rsp SubmitAuditResponseResult
	var req = new(SubmitAuditRequest)
	err := json.Unmarshal([]byte(data), &req)
	if err != nil {
		beego.Critical(err.Error())
		return 0,-6, err
	}
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("POST", serverUrl, req, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return 0,-5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return 0,rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return rsp.SubmitAuditResponse.AuditId,0, nil
}

// 获取第三方提交的审核版本的审核状态（仅供第三方代小程序调用）
func  GetAuditStatus(authorizerAccessToken string, auditId int) (int, int, error){
	client := core.GetPostJSONClient()
	var rsp GetAuditStatusResponseResult
	var req = new(GetAuditStatusRequest)
	req.AuditId = auditId
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("POST", serverUrl, req, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return 0,-5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return 0,rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return rsp.GetAuditStatusResponse.Status,0, nil
}

// 查询最新一次提交的审核状态（仅供第三方代小程序调用）
func  GetLatestAuditStatus(authorizerAccessToken string) (int, int, error){
	client := core.GetPostJSONClient()
	var rsp GetAuditStatusResponseResult
	serverUrl := fmt.Sprintf("https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=%s",authorizerAccessToken)
	tbdata, err := client.Excute("GET", serverUrl, nil, &rsp)
    beego.Debug("GetLatestAuditStatus:", string(tbdata))
	if err != nil {
		beego.Critical(err.Error())
		return 0,-5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return 0,rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return rsp.GetAuditStatusResponse.Status,0, nil
}

// 发布已通过审核的小程序（仅供第三方代小程序调用）
func  ReleaseCode(authorizerAccessToken string) (int, error){
	client := core.GetPostJSONClient()
	var rsp ReleaseResponseResult
	var req = new(ReleaseRequest)
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("POST", serverUrl, req, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return -5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return 0, nil
}

// 修改小程序线上代码的可见状态（仅供第三方代小程序调用）
func  ChangeVisitStatus(authorizerAccessToken, action string) (int, error){
	client := core.GetPostJSONClient()
	var rsp ChangeVisitStatusResponseResult
	var req = new(ChangeVisitStatusRequest)
	req.Action = action
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("POST", serverUrl, req, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return -5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return 0, nil
}

//获取小程序的第三方提交代码的页面配置（仅供第三方开发者代小程序调用）
func  GetPage(authorizerAccessToken string) (string, int, error){
	client := core.GetPostJSONClient()
	var rsp GetPageResponseResult
	var req = new(GetPageRequest)
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("GET", serverUrl, nil, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "",-5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return string(tbdata),0, nil
}

